/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package entityprojet;

import entityprojet.exceptions.NonexistentEntityException;
import java.io.Serializable;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;

/**
 *
 * @author T.Ron
 */
public class JoperiodeJpaController implements Serializable {

    public JoperiodeJpaController(EntityManagerFactory emf) {
        this.emf = emf;
    }
    private EntityManagerFactory emf = Persistence.createEntityManagerFactory("projetT_RPU");

    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }

    public void create(Joperiode joperiode) {
        if (joperiode.getEcriturecCollection() == null) {
            joperiode.setEcriturecCollection(new ArrayList<Ecriturec>());
        }
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Periode periode = joperiode.getPeriode();
            if (periode != null) {
                periode = em.getReference(periode.getClass(), periode.getPeriodePK());
                joperiode.setPeriode(periode);
            }
            Journaux numjo = joperiode.getNumjo();
            if (numjo != null) {
                numjo = em.getReference(numjo.getClass(), numjo.getNumjo());
                joperiode.setNumjo(numjo);
            }
            Collection<Ecriturec> attachedEcriturecCollection = new ArrayList<Ecriturec>();
            for (Ecriturec ecriturecCollectionEcriturecToAttach : joperiode.getEcriturecCollection()) {
                ecriturecCollectionEcriturecToAttach = em.getReference(ecriturecCollectionEcriturecToAttach.getClass(), ecriturecCollectionEcriturecToAttach.getIdEc());
                attachedEcriturecCollection.add(ecriturecCollectionEcriturecToAttach);
            }
            joperiode.setEcriturecCollection(attachedEcriturecCollection);
            em.persist(joperiode);
            if (periode != null) {
                periode.getJoperiodeCollection().add(joperiode);
                periode = em.merge(periode);
            }
            if (numjo != null) {
                numjo.getJoperiodeCollection().add(joperiode);
                numjo = em.merge(numjo);
            }
            for (Ecriturec ecriturecCollectionEcriturec : joperiode.getEcriturecCollection()) {
                Joperiode oldIdJopOfEcriturecCollectionEcriturec = ecriturecCollectionEcriturec.getIdJop();
                ecriturecCollectionEcriturec.setIdJop(joperiode);
                ecriturecCollectionEcriturec = em.merge(ecriturecCollectionEcriturec);
                if (oldIdJopOfEcriturecCollectionEcriturec != null) {
                    oldIdJopOfEcriturecCollectionEcriturec.getEcriturecCollection().remove(ecriturecCollectionEcriturec);
                    oldIdJopOfEcriturecCollectionEcriturec = em.merge(oldIdJopOfEcriturecCollectionEcriturec);
                }
            }
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void edit(Joperiode joperiode) throws NonexistentEntityException, Exception {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Joperiode persistentJoperiode = em.find(Joperiode.class, joperiode.getIdJop());
            Periode periodeOld = persistentJoperiode.getPeriode();
            Periode periodeNew = joperiode.getPeriode();
            Journaux numjoOld = persistentJoperiode.getNumjo();
            Journaux numjoNew = joperiode.getNumjo();
            Collection<Ecriturec> ecriturecCollectionOld = persistentJoperiode.getEcriturecCollection();
            Collection<Ecriturec> ecriturecCollectionNew = joperiode.getEcriturecCollection();
            if (periodeNew != null) {
                periodeNew = em.getReference(periodeNew.getClass(), periodeNew.getPeriodePK());
                joperiode.setPeriode(periodeNew);
            }
            if (numjoNew != null) {
                numjoNew = em.getReference(numjoNew.getClass(), numjoNew.getNumjo());
                joperiode.setNumjo(numjoNew);
            }
            Collection<Ecriturec> attachedEcriturecCollectionNew = new ArrayList<Ecriturec>();
            for (Ecriturec ecriturecCollectionNewEcriturecToAttach : ecriturecCollectionNew) {
                ecriturecCollectionNewEcriturecToAttach = em.getReference(ecriturecCollectionNewEcriturecToAttach.getClass(), ecriturecCollectionNewEcriturecToAttach.getIdEc());
                attachedEcriturecCollectionNew.add(ecriturecCollectionNewEcriturecToAttach);
            }
            ecriturecCollectionNew = attachedEcriturecCollectionNew;
            joperiode.setEcriturecCollection(ecriturecCollectionNew);
            joperiode = em.merge(joperiode);
            if (periodeOld != null && !periodeOld.equals(periodeNew)) {
                periodeOld.getJoperiodeCollection().remove(joperiode);
                periodeOld = em.merge(periodeOld);
            }
            if (periodeNew != null && !periodeNew.equals(periodeOld)) {
                periodeNew.getJoperiodeCollection().add(joperiode);
                periodeNew = em.merge(periodeNew);
            }
            if (numjoOld != null && !numjoOld.equals(numjoNew)) {
                numjoOld.getJoperiodeCollection().remove(joperiode);
                numjoOld = em.merge(numjoOld);
            }
            if (numjoNew != null && !numjoNew.equals(numjoOld)) {
                numjoNew.getJoperiodeCollection().add(joperiode);
                numjoNew = em.merge(numjoNew);
            }
            for (Ecriturec ecriturecCollectionOldEcriturec : ecriturecCollectionOld) {
                if (!ecriturecCollectionNew.contains(ecriturecCollectionOldEcriturec)) {
                    ecriturecCollectionOldEcriturec.setIdJop(null);
                    ecriturecCollectionOldEcriturec = em.merge(ecriturecCollectionOldEcriturec);
                }
            }
            for (Ecriturec ecriturecCollectionNewEcriturec : ecriturecCollectionNew) {
                if (!ecriturecCollectionOld.contains(ecriturecCollectionNewEcriturec)) {
                    Joperiode oldIdJopOfEcriturecCollectionNewEcriturec = ecriturecCollectionNewEcriturec.getIdJop();
                    ecriturecCollectionNewEcriturec.setIdJop(joperiode);
                    ecriturecCollectionNewEcriturec = em.merge(ecriturecCollectionNewEcriturec);
                    if (oldIdJopOfEcriturecCollectionNewEcriturec != null && !oldIdJopOfEcriturecCollectionNewEcriturec.equals(joperiode)) {
                        oldIdJopOfEcriturecCollectionNewEcriturec.getEcriturecCollection().remove(ecriturecCollectionNewEcriturec);
                        oldIdJopOfEcriturecCollectionNewEcriturec = em.merge(oldIdJopOfEcriturecCollectionNewEcriturec);
                    }
                }
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                Integer id = joperiode.getIdJop();
                if (findJoperiode(id) == null) {
                    throw new NonexistentEntityException("The joperiode with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void destroy(Integer id) throws NonexistentEntityException {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Joperiode joperiode;
            try {
                joperiode = em.getReference(Joperiode.class, id);
                joperiode.getIdJop();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The joperiode with id " + id + " no longer exists.", enfe);
            }
            Periode periode = joperiode.getPeriode();
            if (periode != null) {
                periode.getJoperiodeCollection().remove(joperiode);
                periode = em.merge(periode);
            }
            Journaux numjo = joperiode.getNumjo();
            if (numjo != null) {
                numjo.getJoperiodeCollection().remove(joperiode);
                numjo = em.merge(numjo);
            }
            Collection<Ecriturec> ecriturecCollection = joperiode.getEcriturecCollection();
            for (Ecriturec ecriturecCollectionEcriturec : ecriturecCollection) {
                ecriturecCollectionEcriturec.setIdJop(null);
                ecriturecCollectionEcriturec = em.merge(ecriturecCollectionEcriturec);
            }
            em.remove(joperiode);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public List<Joperiode> findJoperiodeEntities() {
        return findJoperiodeEntities(true, -1, -1);
    }

    public List<Joperiode> findJoperiodeEntities(int maxResults, int firstResult) {
        return findJoperiodeEntities(false, maxResults, firstResult);
    }

    private List<Joperiode> findJoperiodeEntities(boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            cq.select(cq.from(Joperiode.class));
            Query q = em.createQuery(cq);
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }

    public Joperiode findJoperiode(Integer id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(Joperiode.class, id);
        } finally {
            em.close();
        }
    }

    public int getJoperiodeCount() {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            Root<Joperiode> rt = cq.from(Joperiode.class);
            cq.select(em.getCriteriaBuilder().count(rt));
            Query q = em.createQuery(cq);
            return ((Long) q.getSingleResult()).intValue();
        } finally {
            em.close();
        }
    }
    
}
